<template>
  <div class="article-comment">
    <!-- 全部评论 -->
    <van-sticky offset-top="11.73333vw">
      <div class="title van-hairline--bottom">
        <span>全部评论 ({{ article.comm_count }})</span>
        <span>{{ article.like_count }} 点赞</span>
      </div>
    </van-sticky>
    <!-- 评论列表 -->
    <div class="list">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        :error="error"
        @load="onLoad"
      >
        <div
          class="item van-hairline--bottom"
          v-for="i in list"
          :key="i.com_id"
        >
          <van-image round width="10vw" height="10vw" :src="i.aut_photo" />
          <div class="info">
            <p>
              <span class="name">{{ i.aut_name }}</span>
              <span class="zan"
                >{{ i.like_count }}
                <i
                  :class="i.is_liking ? 'icon-like-sel' : 'icon-like2'"
                  class="iconfont"
              /></span>
            </p>
            <p class="cont">{{ i.content }}</p>
            <p>
              <span class="reply" @click="handReply(i)"
                >{{ i.reply_count }}回复 <i class="van-icon van-icon-arrow"></i
              ></span>
              <span class="time">{{ i.pubdate | relativeTime }}</span>
            </p>
          </div>
        </div>
      </van-list>
      <div class="title van-hairline--bottom">
        <span>全部评论 ({{ article.comm_count }})</span>
        <span>{{ article.like_count }} 点赞</span>
      </div>
    </div>
    <!-- 底部工具 -->
    <div class="footer van-hairline--top">
      <div class="input" @click="showInput=true"><i class="van-icon van-icon-edit"></i></div>
      <div class="btn" @click="$emit('click-comment')">
        <i class="iconfont icon-comment"></i>
        <p>评论</p>
        <i v-if="article.comm_count">{{ article.comm_count }}</i>
      </div>
      <div class="btn" @click="handLike">
        <i
          class="iconfont"
          :class="article.attitude === 1 ? 'icon-like-sel' : 'icon-like'"
        ></i>
        <p>点赞</p>
      </div>
      <div class="btn" @click="handCollect">
        <i
          class="iconfont"
          :class="article.is_collected ? 'icon-collect-sel' : 'icon-collect'"
        ></i>
        <p>收藏</p>
      </div>
      <div class="btn">
        <i class="iconfont icon-share"></i>
        <p>分享</p>
      </div>
    </div>
    <!-- 回复弹层 -->
    <van-popup v-model="open" position="right"
    @closed="currentComments = {}">
      <!-- 头 -->
      <van-nav-bar left-arrow @click-left="open = false" :title="`${currentComments.reply_count}`" />
      <div class="reply-wrapper list">
        <!-- 列表 -->
        <van-list
          v-model="popUpLoading"
          :finished="popUpFinished"
          finished-text="没有更多了"
          @load="popUpOnLoad"
          :error="popUpError"
          :immediate-check="false"
        >
          <div class="item van-hairline--bottom" v-for="i in popUpList" :key="i.com_id">
            <van-image
              round
              width="10vw"
              height="10vw"
              :src="i.aut_photo"
            />
            <div class="info">
              <p>
                <span class="name">{{ i.aut_name }}</span>
                <span class="zan">{{ i.like_count }}<i class="iconfont icon-like2"></i></span>
              </p>
              <p class="cont">{{ i.content }}</p>
              <p><span class="time" style="margin-left: 0">{{ i.pubdate | relativeTime }}</span></p>
            </div>
          </div>
        </van-list>
      </div>
      <!-- 底 -->
      <div class="footer van-hairline--top" style="position: static">
        <div class="input big" @click="showInput=true"><i class="van-icon van-icon-edit"></i></div>
        <div class="btn">
          <i class="iconfont icon-collect"></i>
          <p>收藏</p>
        </div>
        <div class="btn">
          <i class="iconfont icon-share"></i>
          <p>分享</p>
        </div>
      </div>
    </van-popup>
       <!-- 评论&回复 -->
       <van-popup v-model="showInput" position="bottom">
      <van-nav-bar
        left-arrow
        @click-left="showInput = false"
        :title="currentComments.com_id ? '回复评论' : '评论文章'"
        right-text="发表"
        @click-right="issueComment"
      />
      <van-field
        v-model="text"
        rows="3"
        autosize
        type="textarea"
        maxlength="100"
        :placeholder="currentComments.com_id ? `@${currentComments.aut_name}` : '请输入评论'"
        show-word-limit
      />
    </van-popup>
  </div>
</template>

<script>
import { getCommentsByArticle } from '@/api/channel'
import { likeArticle, collectArticle, commentOrReply } from '@/api/article'
import { getReplysByComment } from '@/api/channel'

export default {
  name: 'ArticleComment',
  data () {
    return {
      loading: false,
      finished: false,
      error: false,
      list: [],
      offset: null,
      currentComments: {},
      open: false,
      popUpLoading: false,
      popUpFinished: false,
      popUpError: false,
      popUpOffSet: null,
      popUpList: [],
      // 显示输入弹窗
      showInput: false,
      // 输入框值
      text: ''
    }
  },
  created () {
    // 开启加载中效果
    this.loading = true
    this.onLoad()
  },
  props: {
    article: {
      require: true,
      type: Object
    }
  },
  methods: {
    async onLoad () {
      const [err, res] = await getCommentsByArticle(
        this.article.art_id,
        this.offset
      )
      if (err) this.error = true
      else {
        if (res.data.data.last_id === res.data.data.end_id) {
          this.finished = true
        } else this.offset = res.data.data.last_id
        this.list.push(...res.data.data.results)
      }
      this.loading = false
    },
    async handLike () {
      if (this.article.attitude === 1) {
        // 取消点赞
        const [err] = await likeArticle(this.article.art_id, false)
        if (err) return this.$toast.fail('取消点赞失败')
        else {
          // eslint-disable-next-line vue/no-mutating-props
          this.article.attitude = -1
          this.$toast.success('取消点赞成功')
        }
      } else {
        // 点赞
        const [err] = await likeArticle(this.article.art_id, true)
        if (err) return this.$toast.fail('点赞失败')
        else {
          // eslint-disable-next-line vue/no-mutating-props
          this.article.attitude = 1
          this.$toast.success('点赞成功')
        }
      }
    },
    async handCollect () {
      if (this.article.is_collected) {
        const [err] = await collectArticle(this.article.art_id, true)
        if (err) return this.$toast.fail('取消收藏失败')
        else {
          // eslint-disable-next-line vue/no-mutating-props
          this.article.is_collected = false
          this.$toast.success('取消点赞成功')
        }
      } else {
        const [err] = await collectArticle(this.article.art_id, false)
        if (err) return this.$toast.fail('收藏失败')
        else {
          // eslint-disable-next-line vue/no-mutating-props
          this.article.is_collected = true
          this.$toast.success('收藏成功')
        }
      }
    },
    handReply (i) {
    //   this.popUpLoading = false
      this.popUpFinished = false
      this.popUpError = false
      this.popUpList = []
      this.popUpOffSet = null
      this.currentComments = i
      this.popUpLoading = true
      this.popUpOnLoad()
      this.open = true
    },
    async popUpOnLoad () {
      const [err, res] = await getReplysByComment(this.currentComments.com_id, this.popUpOffSet)
      if (err) this.popUpError = true
      else {
        this.popUpList.push(...res.data.data.results)
        if (res.data.data.end_id === res.data.data.last_id) {
          this.popUpFinished = true
        } else {
          this.popUpOffSet = res.data.data.last_id
        }
      }
      this.popUpLoading = false
    },
    async issueComment () {
      if (!this.currentComments.com_id) {
        if (!this.text) return this.$toast('请输入评论')
        const [err, res] = await commentOrReply(this.article.art_id, this.text)
        if (err) return this.$toast.fail('评论失败')
        this.list.unshift(res.data.data.new_obj)
        // eslint-disable-next-line vue/no-mutating-props
        this.article.comm_count++
        this.showInput = false
        this.text = ''
        this.$toast.success('评论成功')
      } else {
        // 2. 提交评论回复
        // 2.1. 校验是否输入内容
        // 2.2. 调用接口
        // 2.3. 失败提示
        // 2.4. 更新回复列表+更新评论数量+更新回复数量+关闭对话框+清除输入内容+成功提示
        if (!this.text) return this.$toast('请输入回复')
        const [err, res] = await commentOrReply(
          this.currentComments.com_id,
          this.text,
          this.article.art_id
        )
        if (err) return this.$toast.fail('回复失败')
        this.popUpList.unshift(res.data.data.new_obj)
        // eslint-disable-next-line vue/no-mutating-props
        this.article.comm_count++
        this.currentComments.reply_count++
        this.showInput = false
        this.text = ''
        this.$toast.success('回复成功')
      }
    }
  }
}
</script>

  <style scoped lang="less">
  ::v-deep .van-nav-bar__text {
  color: @geek-color;
}
::v-deep .van-field__control {
  background: @geek-gray-color;
  padding: 5px 10px;
  margin-bottom: 5px;
  border-radius: 4px;
}
.footer {
  position: fixed;
  left: 0;
  bottom: 0;
  height: 50px;
  background: #fff;
  display: flex;
  width: 100%;
  align-items: center;
  .input {
    margin-left: 10px;
    width: 200px;
    height: 34px;
    background: @geek-gray-color;
    border-radius: 17px;
    line-height: 36px;
    padding-left: 10px;
    &.big {
      width: 100%;
      margin-right: 10px;
    }
    .van-icon {
      color: #999;
    }
  }
  .btn {
    flex: 1;
    text-align: center;
    position: relative;
    p {
      margin: 0;
      font-size: 10px;
    }
    .iconfont {
      font-size: 18px;
    }
    i:not(.iconfont) {
      height: 16px;
      min-width: 16px;
      padding: 0 3px;
      background: @geek-color;
      color: #fff;
      font-size: 10px;
      position: absolute;
      right: 0;
      top: -4px;
      line-height: 16px;
      border-radius: 8px;
      font-style: normal;
    }
  }
}
.article-comment {
  .title {
    height: 50px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 16px;
    background: #fff;
    span {
      font-size: 16px;
      &:last-child {
        color: #ccc;
        font-size: 14px;
      }
    }
  }
  .list {
    padding: 0 16px;
    .item {
      display: flex;
      padding: 10px 0;
      .info {
        padding-left: 10px;
        flex: 1;
        p {
          margin: 0;
          .name {
            font-size: 16px;
          }
          .zan {
            font-size: 14px;
            float: right;
            color: #999;
            margin-right: 20px;
            .i {
              font-size: 12px;
              position: relative;
              top: -1px;
            }
          }
          &.cont {
            font-size: 14px;
            color: #666;
            padding: 10px 0;
            word-break: break-all;
            padding-right: 40px;
          }
          .reply {
            min-width: 60px;
            height: 24px;
            text-align: center;
            line-height: 28px;
            font-size: 12px;
            background: @geek-gray-color;
            display: inline-block;
            border-radius: 14px;
            color: #666;
            .van-icon {
              position: relative;
              top: 1px;
            }
          }
          .time {
            font-size: 12px;
            color: #999;
            margin-left: 10px;
          }
        }
      }
    }
  }
}
.van-popup {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.reply-wrapper {
  width: 100%;
  height: 100%;
  overflow-y: auto;
}
</style>
